package com.humanmonth.home.component.page.signature
{
	import flash.display.CapsStyle;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	
	/**
	 * 实现手写签名的白板
	 * @author 林子龙
	 * 
	 */
	public class WriteArea extends Canvas
	{
		/**
		 *笔 
		 */
		public var signature:UIComponent=new UIComponent();
		/**
		 *颜色 
		 */
		public var myColor:uint=0x000000;
		/**
		 *线条粗细 
		 */
		public var lineSize:int=1;
		/**
		 *模式 
		 */
		public var pattern:String="圆珠笔";
		/**
		 *当前的x座标 
		 */
		private var cX:Number;
		/**
		 *当前的y座标 
		 */
		private var cY:Number;
		
		public function WriteArea()
		{
			this.addChild(signature);
			this.addEventListener(MouseEvent.MOUSE_DOWN,beginDraw);
			this.addEventListener(MouseEvent.MOUSE_UP,endDraw);
		}
		
		/**
		 *鼠标压下时，开始画图，并添加移动鼠标画线的监听器 
		 */
		private function beginDraw(event:MouseEvent):void{
			this.signature.graphics.lineStyle(lineSize,myColor,1,true,LineScaleMode.NONE,CapsStyle.ROUND,JointStyle.ROUND,99);
			this.signature.graphics.beginFill(myColor);
			this.cX=event.localX;
			this.cY=event.localY;
			this.signature.graphics.moveTo(this.cX,this.cY);
			this.addEventListener(MouseEvent.MOUSE_MOVE,drawIng);
		}
		
		/**
		 * 鼠标移动时，画线 
		 */
		private function drawIng(event:MouseEvent):void{
			if(this.pattern=="圆珠笔"){
				this.signature.graphics.moveTo(this.cX,this.cY);
			}
			this.signature.graphics.lineTo(event.localX,event.localY);
			this.cX=event.localX;
			this.cY=event.localY;
		}
		
		/**
		 * 结束画图 
		 */
		private function endDraw(event:MouseEvent):void{
			this.removeEventListener(MouseEvent.MOUSE_MOVE,drawIng);
		}
		
	}
}